﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Model;

namespace DAL
{
    public class BookService
    {
        public List<BookInfo> QueryAllBook()
        {
            #region xxx
            //操作数据库：ADO.NET()
            //SqlConnection：连接数据库
            //SqlCommand:执行SQL命令
            //SqlDataReader:读取执行SQL产生的结果
            SqlConnection conn = new SqlConnection("Data Source=.;Integrated Security=True");
            string sql = "select * from BookInfo";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            //判断是否查询到数据
            //if (dr.Read())//判断有数据()
            //{

            //}
            List<BookInfo> list = new List<BookInfo>();
            //如果要全部读取查询到数据
            while (dr.Read())
            {
                BookInfo bi = new BookInfo();
                //一个变量在内容中大概占用4字节
                //object o = null;
                //将数据库数据转换成实体(给对象赋值)
                bi.BookId = Convert.ToInt32(dr["BookId"]);
                bi.BookName = dr["BookName"].ToString();
                bi.BookPrice = Convert.ToDouble(dr["BookPrice"]);
                bi.Publisher = dr["Publisher"].ToString();
                list.Add(bi);//将数据添加到集合

            }
            dr.Close();
            conn.Close();
            #endregion
            return list;
        }
    }
}
